package com.sunda.spmsweb.ordercontroller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsorder.service.IOrderCostDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import localhost.services.DeptsData.DeptsDataPortTypeProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 订单费用承担部门 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-04-20
 */
@RestController
@RequestMapping("/orderCostDept")
@Api(tags = "OA费用承担部门码值信息", description = "OA费用承担部门码值信息")
public class OrderCostDeptController {

    @Autowired
    IOrderCostDeptService iOrderCostDeptService;

    @GetMapping("/getOrderCostDept")
    @ApiOperation(value = "获取有效费用承担部门码值信息", notes = "获取有效费用承担部门码值信息，前端取数只取 父级部门id 大于等于0 的数据即可。")
    public ResponseResult getOrderCostDept(){
        try {
            return ResponseResult.success().add("orderCostDeptList", iOrderCostDeptService.getOrderCostDept());
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("获取数据失败");
        }
    }

    @GetMapping("/getOrderCostDeptLevel")
    @ApiOperation(value = "获取有效费用承担部门分级数据", notes = "获取全部根节点为 0 的分级 费用承担部门 数据")
    public ResponseResult getOrderCostDeptLevel(){
        try {
            return ResponseResult.success().add("orderCostDeptList", iOrderCostDeptService.getOrderCostDeptLevel());
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("获取数据失败");
        }
    }

    @GetMapping("/updateOderCostDept")
    @ApiOperation(value = "从OA同步费用承担部门码值信息", notes = "从OA同步费用承担部门码值信息")
    public ResponseResult updateOderCostDept(){
        try {
            DeptsDataPortTypeProxy deptsDataPortTypeProxy = new DeptsDataPortTypeProxy();
            JSONArray array = JSONArray.parseArray(JSON.toJSON(deptsDataPortTypeProxy.getDepts()).toString());
            int result = iOrderCostDeptService.updateOderCostDept(array);
            if (result == 0){
                return ResponseResult.error("请求OA更新数据失败");
            }
            return ResponseResult.success().add("更新数据条数：", result);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求OA更新数据失败");
        }
    }

}
